package com.zgr.begin;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.SwingUtilities;

import com.alibaba.simpleimage.render.ScaleParameter;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class BeginFrame extends javax.swing.JFrame {
	private JTextField widthText;
	private JLabel widthLabel;
	private JTextField heightText;
	private JProgressBar jobProcessBar;
	private JLabel chooseFileLabel;
	private JTextField choosedFileText;
	private JButton chooseFileButton;
	private JLabel heightLabel;
	private JButton execButton;
	private JFileChooser fileChooser;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				BeginFrame inst = new BeginFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public BeginFrame() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(
					(JComponent) getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("\uc774\uc124\uacbd\uc0ac\ub791\ud574");
			{
				widthText = new JTextField();
			}
			{
				widthLabel = new JLabel();
				widthLabel.setText("\u5bbd\u5ea6");
			}
			{
				heightLabel = new JLabel();
				heightLabel.setText("\u9ad8\u5ea6");
			}
			{
				chooseFileButton = new JButton();
				chooseFileButton.setText("...");
				chooseFileButton.addActionListener(getActionListener());
				fileChooser = new JFileChooser();
				fileChooser.setMultiSelectionEnabled(true);
			}
			{
				choosedFileText = new JTextField();
			}
			{
				chooseFileLabel = new JLabel();
				chooseFileLabel.setText("\u9009\u62e9\u6587\u4ef6");
			}
			{
				jobProcessBar = new JProgressBar();
				jobProcessBar.setStringPainted(true);
				jobProcessBar.setForeground(Color.ORANGE);
			}
			{
				heightText = new JTextField();
			}
			{
				execButton = new JButton();
				execButton.setText("\u5f00\u59cb");
				execButton.addActionListener(getActionListener());
			}
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout
									.createParallelGroup(
											GroupLayout.Alignment.BASELINE)
									.addComponent(chooseFileButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(choosedFileText,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 23,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(chooseFileLabel,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 22,
											GroupLayout.PREFERRED_SIZE))
					.addGap(59)
					.addGroup(
							thisLayout
									.createParallelGroup(
											GroupLayout.Alignment.BASELINE)
									.addComponent(widthText,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(widthLabel,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 21,
											GroupLayout.PREFERRED_SIZE))
					.addGap(31)
					.addGroup(
							thisLayout
									.createParallelGroup(
											GroupLayout.Alignment.BASELINE)
									.addComponent(execButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(heightText,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(heightLabel,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 19,
											GroupLayout.PREFERRED_SIZE))
					.addGap(0, 100, Short.MAX_VALUE)
					.addComponent(jobProcessBar, GroupLayout.PREFERRED_SIZE,
							20, GroupLayout.PREFERRED_SIZE));
			thisLayout
					.setHorizontalGroup(thisLayout
							.createParallelGroup()
							.addComponent(jobProcessBar,
									GroupLayout.Alignment.LEADING, 0, 500,
									Short.MAX_VALUE)
							.addGroup(
									GroupLayout.Alignment.LEADING,
									thisLayout
											.createSequentialGroup()
											.addGap(19)
											.addGroup(
													thisLayout
															.createParallelGroup()
															.addGroup(
																	GroupLayout.Alignment.LEADING,
																	thisLayout
																			.createSequentialGroup()
																			.addComponent(
																					heightLabel,
																					GroupLayout.PREFERRED_SIZE,
																					48,
																					GroupLayout.PREFERRED_SIZE)
																			.addGap(11))
															.addGroup(
																	GroupLayout.Alignment.LEADING,
																	thisLayout
																			.createSequentialGroup()
																			.addComponent(
																					widthLabel,
																					GroupLayout.PREFERRED_SIZE,
																					48,
																					GroupLayout.PREFERRED_SIZE)
																			.addGap(11))
															.addComponent(
																	chooseFileLabel,
																	GroupLayout.Alignment.LEADING,
																	GroupLayout.PREFERRED_SIZE,
																	59,
																	GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(
													LayoutStyle.ComponentPlacement.UNRELATED)
											.addGroup(
													thisLayout
															.createParallelGroup()
															.addGroup(
																	GroupLayout.Alignment.LEADING,
																	thisLayout
																			.createSequentialGroup()
																			.addGroup(
																					thisLayout
																							.createParallelGroup()
																							.addComponent(
																									heightText,
																									GroupLayout.Alignment.LEADING,
																									GroupLayout.PREFERRED_SIZE,
																									81,
																									GroupLayout.PREFERRED_SIZE)
																							.addComponent(
																									widthText,
																									GroupLayout.Alignment.LEADING,
																									GroupLayout.PREFERRED_SIZE,
																									81,
																									GroupLayout.PREFERRED_SIZE))
																			.addGap(0,
																					60,
																					Short.MAX_VALUE)
																			.addComponent(
																					execButton,
																					GroupLayout.PREFERRED_SIZE,
																					62,
																					GroupLayout.PREFERRED_SIZE)
																			.addGap(29))
															.addGroup(
																	thisLayout
																			.createSequentialGroup()
																			.addComponent(
																					choosedFileText,
																					GroupLayout.PREFERRED_SIZE,
																					232,
																					GroupLayout.PREFERRED_SIZE)
																			.addGap(0,
																					0,
																					Short.MAX_VALUE)))
											.addGap(40)
											.addComponent(chooseFileButton,
													GroupLayout.PREFERRED_SIZE,
													77,
													GroupLayout.PREFERRED_SIZE)
											.addContainerGap(55, 55)));
			pack();
			this.setSize(516, 272);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	private ActionListener getActionListener() {
		return new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.out.println("chooseFileButton.actionPerformed, event="
						+ evt);
				if (evt.getSource().equals(chooseFileButton)) {
					int state = fileChooser.showOpenDialog(null);
					if (state == 1) {
						return;
					} else {
						File files[] = fileChooser.getSelectedFiles();
						StringBuilder sFiles = new StringBuilder();
						for (File eachFile : files) {
							sFiles.append(eachFile.getAbsolutePath()).append(
									",");
						}
						sFiles.delete(sFiles.length() - 1, sFiles.length());
						choosedFileText.setText(sFiles.toString());
					}
				} else if (evt.getSource().equals(execButton)) {
					new Thread(new Runnable() {
						@Override
						public void run() {
							execCompressImg(choosedFileText.getText());
						}
					}).start();
				}
			}
		};
	}

	private void execCompressImg(String imgs) {
		String imgFiles[] = imgs.split(",");
		int desiredWidth = 0, desiredHeight = 0;
		String outputFile = null;
		ScaleParameter scaleParameter = null;
		boolean format = false;
		int increment = 0;

		try {
			desiredWidth = Integer.valueOf(widthText.getText());
		} catch (NumberFormatException e) {
			format = true;
			desiredWidth = 800;
		}

		try {
			desiredHeight = Integer.valueOf(heightText.getText());
		} catch (NumberFormatException e) {
			format = true;
			desiredHeight = 800;
		}

		if (format) {
			desiredWidth = desiredHeight = (desiredWidth > desiredHeight ? desiredWidth
					: desiredHeight);
		}
		scaleParameter = ImageScaleUti.getParam(desiredWidth, desiredHeight);

		jobProcessBar.repaint();
		jobProcessBar.setMinimum(0);
		jobProcessBar.setMaximum(imgFiles.length);
		jobProcessBar.setValue(increment++);
		for (String eachImg : imgFiles) {
			File imgFile = new File(eachImg);
			if (!imgFile.exists() || imgFile.isDirectory()) {
				continue;
			}

			if (eachImg.length() < 4) {
				continue;
			}

			outputFile = imgFile.getParent() + "/output/" + imgFile.getName();

			try {
				ImageScaleUti.scaleRenderImage(eachImg, outputFile,
						scaleParameter);
			} catch (Exception e) {
				jobProcessBar.setValue(2);
				e.printStackTrace();
			}
			jobProcessBar.setValue(increment++);
		}

	}

}
